package com.go.where.manager.action.go;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;

import com.go.where.manager.action.SuperAction;
import com.go.where.manager.common.Constants;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.BannerInfo;
import com.go.where.manager.service.BannerService;
import com.go.where.manager.util.CommonUtil;
import com.go.where.manager.util.CompressPicUtil;
import com.go.where.manager.util.DateUtil;

@InterceptorRefs( {
		@InterceptorRef(value = "defaultStack", params = { "maximumSize",
				"2048000", "allowedTypes",
				"image/bmp,image/png,image/gif,image/jpeg" }),
		@InterceptorRef(value = "defalutStackSet") })
public class BannerAction extends SuperAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4383195694100314090L;
	private static final Logger log = Logger.getLogger(BannerAction.class);

	public Pagination<?> page;

	@Resource(name = "bannerService")
	private BannerService bannerService;

	private BannerInfo bannerInfo;
	private BannerInfo bannerInfoQuery;

	private String id;

	private File bannerPic;// 上传的文件
	private String bannerPicFileName; // 文件名称
	private String bannerPicContentType; // 文件类型

	/**
	 * 查询列表
	 * 
	 * @return
	 */
	public String list() {
		if (bannerInfoQuery == null) {
			bannerInfoQuery = new BannerInfo();
			bannerInfoQuery.setPageSize(Constants.INIT_PAGESIZE);
			bannerInfoQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			page = bannerService.queryPage(bannerInfoQuery);
		} catch (Exception e) {
			addActionError(e.getMessage());
			log.error("查询信息异常", e);
		}
		return LIST;
	}

	public String toEdit() {
		bannerInfo = bannerService.queryByPrimartKey(id);
		bannerInfo.setOldBannerOrder(bannerInfo.getBannerOrder());
		return INPUT;
	}

	public String add() {

		return INPUT;
	}

	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() {
		try {
			StringBuilder picPath = new StringBuilder();
			ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
			String realpath = ServletActionContext
					.getServletContext()
					.getRealPath(
							CommonUtil.FILE_SEPARATOR + Constants.BANNER_IMAGES);
			File file = bannerPic;
			if (file != null) {
				createImg(picPath, realpath, file);
			}
			if (picPath != null && picPath.toString().length() > 0) {
				bannerInfo.setBannerPath(picPath.toString());
			}

			bannerService.save(bannerInfo, getCurrentOperator());
			redirectionUrl = "banner!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("保存信息异常", e);
			addActionError("保存信息异常！" + e);
			return this.add();
		}
	}

	/**
	 * 编辑
	 * 
	 * @return
	 */
	public String edit() {
		try {
			StringBuilder picPath = new StringBuilder();
			ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
			String realpath = ServletActionContext
					.getServletContext()
					.getRealPath(
							CommonUtil.FILE_SEPARATOR + Constants.BANNER_IMAGES);
			File file = bannerPic;
			if (file != null) {
				createImg(picPath, realpath, file);
			}
			if (picPath != null && picPath.toString().length() > 0) {
				bannerInfo.setBannerPath(picPath.toString());
			}

			bannerService.update(bannerInfo, getCurrentOperator());
			redirectionUrl = "banner!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("修改信息异常", e);
			addActionError("修改信息异常！" + e);
			id = bannerInfo.getId();
			return this.toEdit();
		}
	}

	/**
	 * 保存图片
	 * 
	 * @param picPath
	 *            保存到数据库路径
	 * @param realpath
	 *            服务器上路径
	 * @param file
	 *            上传文件
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	private void createImg(StringBuilder picPath, String realpath, File file)
			throws FileNotFoundException, IOException {
		File savedir = new File(realpath);
		if (!savedir.exists()) {
			savedir.mkdirs();
		}
		String newFileName = DateUtil.format(new Date(), "yyyyMMddHHmmss")
				+ "_" + CommonUtil.createRandomNumber()
				+ bannerPicFileName.substring(bannerPicFileName.indexOf("."));

		FileOutputStream fos = new FileOutputStream(realpath
				+ CommonUtil.FILE_SEPARATOR + newFileName);
		picPath
				.append(CommonUtil.FILE_SEPARATOR + Constants.BANNER_IMAGES
						+ Constants.ZOOM_PATH + CommonUtil.FILE_SEPARATOR
						+ newFileName);
		CompressPicUtil.compressPicture(realpath, newFileName, file);
		FileInputStream fis = new FileInputStream(file);
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = fis.read(buffer)) > 0) {
			fos.write(buffer, 0, len);
		}
		fis.close();
		fos.close();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public File getBannerPic() {
		return bannerPic;
	}

	public void setBannerPic(File bannerPic) {
		this.bannerPic = bannerPic;
	}

	public String getBannerPicFileName() {
		return bannerPicFileName;
	}

	public void setBannerPicFileName(String bannerPicFileName) {
		this.bannerPicFileName = bannerPicFileName;
	}

	public String getBannerPicContentType() {
		return bannerPicContentType;
	}

	public void setBannerPicContentType(String bannerPicContentType) {
		this.bannerPicContentType = bannerPicContentType;
	}

	public BannerInfo getBannerInfo() {
		return bannerInfo;
	}

	public void setBannerInfo(BannerInfo bannerInfo) {
		this.bannerInfo = bannerInfo;
	}

	public BannerInfo getBannerInfoQuery() {
		return bannerInfoQuery;
	}

	public void setBannerInfoQuery(BannerInfo bannerInfoQuery) {
		this.bannerInfoQuery = bannerInfoQuery;
	}

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

}
